package xyz.eden.boot.core.result;


import java.util.Objects;

/**
 * ResultSetTool
 *
 * @author gavin
 * @version 1.0
 * @descriptor
 * @date 2021/8/5 11:24
 */
public class ResultSetTool {

    /**
     * 业务异常
     *
     * @param resultSet
     * @param <T>
     * @return
     */
    public static <T> Boolean isBusinessError(ResultSet<T> resultSet) {
        if (Objects.isNull(resultSet)) {
            return Boolean.FALSE;
        }
        return Objects.equals(ResultSetEnum.ERROR_BUSINESS.getCode(), resultSet.getCode());
    }

    /**
     * 参数异常
     *
     * @param resultSet
     * @param <T>
     * @return
     */
    public static <T> Boolean isParamError(ResultSet<T> resultSet) {
        if (Objects.isNull(resultSet)) {
            return Boolean.FALSE;
        }
        return Objects.equals(ResultSetEnum.ERROR_PARAM.getCode(), resultSet.getCode());
    }

    /**
     * 判断 success
     *
     * @param resultSet
     * @param <T>
     * @return
     */
    public static <T> Boolean isSuccess(ResultSet<T> resultSet) {
        if (Objects.isNull(resultSet)) {
            return Boolean.FALSE;
        }
        return resultSet.getCode() == ResultSetEnum.SUCCESS.getCode();
    }

    /**
     * 构建 Success
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResultSet<T> builderSuccess(T data) {
        return builderResultSet(ResultSetEnum.SUCCESS.getCode(), ResultSetEnum.SUCCESS.getMsg(), data);
    }

    /**
     * 构建 fail
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> ResultSet<T> builderFail(int code, String msg) {
        return builderResultSet(code, msg, null);
    }

    /**
     * 构建 ResultSet
     *
     * @param code
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResultSet<T> builderResultSet(int code, String msg, T data) {
        return new ResultSet<>(code, msg, data);
    }
}
